package com.self.match.config;

import com.lmax.disruptor.EventHandler;
import com.self.match.disruptor.OrderEvent;
import com.self.match.disruptor.OrderEventHandler;
import com.self.match.model.OrderBooks;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * @version v1.0
 * @ClassName: MatchEngineAutoConfiguration
 * @Description:
 * @Author: Mac
 * @Date: 2022/9/3 22:28
 */
@Configuration
@EnableConfigurationProperties(MatchEngineProperties.class)
public class MatchEngineAutoConfiguration {

    private MatchEngineProperties matchEngineProperties;

    public MatchEngineAutoConfiguration(MatchEngineProperties matchEngineProperties) {
        this.matchEngineProperties = matchEngineProperties;
    }

    @Bean("eventHandlers")
    public EventHandler<OrderEvent>[] eventHandlers(){
        Map<String, MatchEngineProperties.CoinScale> symbols = matchEngineProperties.getSymbols();
        int i = 0;
        EventHandler<OrderEvent>[] eventHandlers = new EventHandler[symbols.size()];
        for (Map.Entry<String, MatchEngineProperties.CoinScale> entry :symbols.entrySet()){
            String symbol = entry.getKey();
            MatchEngineProperties.CoinScale coinScale = entry.getValue();
            OrderBooks orderBooks = null;
            if (coinScale != null){
                orderBooks = new OrderBooks(symbol, coinScale.getCoinScale(), coinScale.getBaseCoinScale());
            }else{
                orderBooks = new OrderBooks(symbol);
            }
            eventHandlers[i++] = new OrderEventHandler(orderBooks);
        }
        return eventHandlers;
    }
}
